/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.Exception.ZCException;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevel;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevelHistory;

/**
 * @author ZhangCheng
 * @time 2019年9月18日
 */
public class CustomerCreditLevelHistoryDAO extends BasicDAO<CustomerCreditLevelHistory> {
	/**
	 * CustomerCreditLevelHistoryDAO无参构造 重载父类构造器
	 */
	public CustomerCreditLevelHistoryDAO() {
		super();
	}

	/**
	 * CustomerCreditLevelHistoryDAO有参构造 重载父类构造器
	 */
	public CustomerCreditLevelHistoryDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * getCusCreditLevelHissByCusId(String cusId) 通过用户Id获取该用户目前的所有历史信用等级表
	 * 
	 * @param cusId 用户id excludeDelete 是否排除已经被标记删除的
	 */
	public ArrayList<CustomerCreditLevelHistory> getCusCreditLevelHissByCusId(String cusId, boolean excludeDelete, boolean isASC) {
		StringBuffer sql = new StringBuffer("select * from CustomerCreditLevelHistory where cusid = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0 ");// 如果排除被删除的，那么只返回未被删除的
		if(isASC) { 
			sql.append("ORDER BY levelHisId ASC");//若为升序
		}else { 
			sql.append("ORDER BY levelHisId DESC");
		}
		ArrayList<CustomerCreditLevelHistory> cCLHiss = getInfoFromDB(sql.toString(), cusId);
		if (cCLHiss.size() == 0) {
			return null;
		} else {
			return cCLHiss;
		}
	}

	/**
	 * deleteCusCreditLevelHisById(String cusId) 通过用户Id删除该客户的信用历史
	 * 
	 * @param cusId 用户id excludeDelete 是否排除已经被标记删除的
	 */
	public int deleteCusCreditLevelHisById(String cusId,boolean excludeDelete,boolean isASC) throws ZCException {
		int count = 0;
		if (getCusCreditLevelHissByCusId(cusId,excludeDelete,isASC) == null) {// 已经被删除或者未及时更新isdelete
			throw new ZCException("没有该客户的当前有效信用等级或该客户上期信用等级未及时更新！");
		} else {
			String sql = "update CustomerCreditLevelHistory set isdelete = 1 where cusid = ?";
			try {
				count = updateDML(sql, cusId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return count;
	}

	/**
	 * insertNewCusCreditHisLevel(CustomerCreditLevelHistory cusCL) 插入已过期的历史表
	 * 参数是信用表已经被删除的数据 插入到历史表中
	 */
	public int insertNewCusCreditHisLevel(CustomerCreditLevel cusCL) {
		int count = 0;
		String hisId = "";
		String sql = "Insert into CustomerCreditLevelHistory values(?,?,?,?,?,?)";

		String sqlMax = "SELECT * FROM CustomerCreditLevelHistory WHERE ID = (SELECT MAX(ID) FROM CustomerCreditLevelHistory)";
		try {
			hisId = Integer.parseInt(super.getInfoFromDB(sqlMax).get(0).getLevelHisId()) + 1 + "";
		} catch (IndexOutOfBoundsException e) {
			hisId = "0";
		}
		try {
			count = super.updateDML(sql, hisId, cusCL.getCusId(), cusCL.getCreditLevel(), cusCL.getThisLevelBeginDate(),
					cusCL.getThisLevelEndDate(), 0);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public ArrayList<CustomerCreditLevelHistory> resultSetToArrayList(ResultSet re) {
		ArrayList<CustomerCreditLevelHistory> cusCLHis = new ArrayList<CustomerCreditLevelHistory>();

		try {
			while (re.next()) {
				cusCLHis.add(new CustomerCreditLevelHistory(re.getString("levelHisId"), re.getInt("cusid"),
						re.getInt("creditLevel"), re.getString("thisLevelBeginDate"),
						re.getString("thisLevelEndDate")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return cusCLHis;
	}
}
